package customize

import (
	"errors"

	"gitcode.com/G-YT/baoyou/server/global"
	"gitcode.com/G-YT/baoyou/server/model/customize"
	customizeReq "gitcode.com/G-YT/baoyou/server/model/customize/request"
	"gitcode.com/G-YT/baoyou/server/utils"
	"gorm.io/gorm"
)

type DiseaseService struct{}

// CreateDisease 创建病症
func (diseaseService *DiseaseService) CreateDisease(disease *customize.Disease) (err error) {
	err = global.GVA_DB.Create(disease).Error
	return err
}

func (diseaseService *DiseaseService) DeleteDisease(ID string) (err error) {
	var count int64
	err = global.GVA_DB.Find(&customize.Disease{}, "parent_id = ?", ID).Count(&count).Error
	if count > 0 {
		return errors.New("此节点存在子节点不允许删除")
	}
	if err != nil {
		return err
	}
	err = global.GVA_DB.Delete(&customize.Disease{}, "id = ?", ID).Error
	return err
}

// DeleteDiseaseByIds 批量删除
func (diseaseService *DiseaseService) DeleteDiseaseByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]customize.Disease{}, "id in ?", IDs).Error
	return err
}

// UpdateDisease 更新病症
func (diseaseService *DiseaseService) UpdateDisease(disease customize.Disease) (err error) {
	err = global.GVA_DB.Model(&customize.Disease{}).Where("id = ?", disease.ID).Updates(&disease).Error
	return err
}

// GetDisease 根据ID获取病症
func (diseaseService *DiseaseService) GetDisease(ID string) (disease customize.Disease, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&disease).Error
	return
}

// GetDiseaseListByParentID 根据病症类型ID获取病症列表
func (diseaseService *DiseaseService) GetDiseaseListByParentID(IDs []string) (list []customize.Disease, err error) {
	err = global.GVA_DB.Where("parent_id IN (?)", IDs).Find(&list).Error
	return
}

// GetDiseaseTypeList 获取病症类型列表
func (diseaseService *DiseaseService) GetDiseaseTypeList() (list []customize.Disease, err error) {
	err = global.GVA_DB.Where("parent_id = 0").Find(&list).Error
	return
}

// GetDiseaseInfoList 获取病症列表(树形)
func (diseaseService *DiseaseService) GetDiseaseInfoList() (list []*customize.Disease, err error) {
	// 创建db
	db := global.GVA_DB.Model(&customize.Disease{})
	var diseases []*customize.Disease

	err = db.Find(&diseases).Error

	return utils.BuildTree(diseases), err
}

// SetDiseaseArticle 设置病症文章关系
func (diseaseService *DiseaseService) SetDiseaseArticle(req customizeReq.DiseaseArticleReq) (err error) {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("disease_id = ?", req.DiseaseID).Delete(&customize.DiseaseArticle{}).Error; err != nil {
			return err
		}
		var diseaseIDArticles []customize.DiseaseArticle
		for _, richText := range req.RichTexts {
			diseaseIDArticles = append(diseaseIDArticles, customize.DiseaseArticle{
				DiseaseID: req.DiseaseID,
				ArticleID: richText,
				Type:      "richText",
			})
		}
		for _, audio := range req.Audios {
			diseaseIDArticles = append(diseaseIDArticles, customize.DiseaseArticle{
				DiseaseID: req.DiseaseID,
				ArticleID: audio,
				Type:      "audio",
			})
		}
		for _, video := range req.Videos {
			diseaseIDArticles = append(diseaseIDArticles, customize.DiseaseArticle{
				DiseaseID: req.DiseaseID,
				ArticleID: video,
				Type:      "video",
			})
		}
		if len(diseaseIDArticles) == 0 {
			return nil
		}
		return tx.Create(&diseaseIDArticles).Error
	})
}

// GetDiseaseArticle 获取病症文章关系
func (diseaseService *DiseaseService) GetDiseaseArticle(diseaseID uint) (list []customize.DiseaseArticle, err error) {
	err = global.GVA_DB.Where("disease_id = ?", diseaseID).Find(&list).Error
	return
}
